<?php
/**
 * 
 * Copyright (c) 2010, SRIT Stefan Riedel <info@srit.biz>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 * - Redistributions of source code must retain the above copyright notice, 
 * this list of conditions and the following disclaimer.
 * - Redistributions in binary form must reproduce the above copyright notice, 
 * this list of conditions and the following disclaimer in the documentation 
 * and/or other materials provided with the distribution.
 * - Neither the name of the author nor the names of its 
 * contributors may be used to endorse or promote products derived from 
 * this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 
 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, 
 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * @category  TK
 * @package	  AsteriskApi
 * @author    Stefan Riedel <sr@srit.biz>
 * @copyright 2010 SRIT Stefan Riedel
 * @license   http://www.opensource.org/licenses/bsd-license.php New BSD License
 */

require_once 'IAsteriskApi.php';
require_once 'CRMAsteriskApiException.php';
require_once 'CRMAsteriskApiBase.php';

/**
 * @category  TK
 * @package	  AsteriskApi
 * @author    Stefan Riedel <sr@srit.biz>
 * @copyright 2010 SRIT Stefan Riedel
 * @license   http://www.opensource.org/licenses/bsd-license.php New BSD License
 * @author Stefan Riedel
 */
class CRMAsteriskApiCall extends CRMAsteriskApiBase implements IAsteriskApiCall {
	public function callTo($to, $channel, $context, $callerId, $prio = 1, $timeout = 30000, array $variables = array(), $actionId = null) {
		
		$this->_checkSocket ();
		if (! $this->isLoggedIn ()) {
			throw new CRMAsteriskApiException ( 'Bitte vorher einen Login ausführen!' );
		}
		
		$to = $this->prepareNumberForAsterisk($to);
		/**
		 * @todo refaktorieren standard strings wie channel, context, exten, callerid usw. in methoden auslagern
		 * @var string
		 */
		$command = "Action: Originate" . self::END_OF_LINE . "Channel: " . $channel . self::END_OF_LINE . "Context: " . $context . self::END_OF_LINE . "Exten: " . $to . self::END_OF_LINE . "Priority: " . $prio . self::END_OF_LINE . "Callerid: " . $callerId . self::END_OF_LINE . "Timeout: " . $timeout . self::END_OF_LINE;
		
		if ($variables !== array ()) {
			$vars = array ();
			foreach ( $variables as $var => $value ) {
				$vars [] = $var . '=' . $value;
			}
			$vars = implode ( '|', $vars );
			$command .= "Variable: " . $vars . self::END_OF_LINE;
		}
		
		if (! empty ( $actionId )) {
			$command .= "ActionID: " . $actionId . self::END_OF_LINE;
		}
		$command . self::END_OF_LINE;
		$response = $this->_sendCommandToSocket ( $command . self::END_OF_LINE );
		return $response;
	}
	public function hangup($channel) {
		$this->_checkSocket ();
		if (! $this->isLoggedIn ()) {
			throw new CRMAsteriskApiException ( 'Bitte vorher einen Login ausführen!' );
		}
		$command = "Action: Hangup" . self::END_OF_LINE . "Channel: " . $channel . self::DOUBLE_END_OF_LINE;
		$response = $this->_sendCommandToSocket ( $command );
		return $response;
	}
	
	public function getSipPeers() {
		$this->_checkSocket ();
		if (! $this->isLoggedIn ()) {
			throw new CRMAsteriskApiException ( 'Bitte vorher einen Login ausführen!' );
		}
		$command = "Action: Sippeers" . self::DOUBLE_END_OF_LINE;
		$response = $this->_sendCommandToSocket ( $command, false );
		return $response;	
	}
	
	public function redirect($channel, $to, $context) {
	
	}
	public function monitor($channel, $fileName = '', $mix = 0) {
	
	}
	public function stopMonitor($channel) {
	
	}
	public function status($channel) {
		$this->_checkSocket ();
		if (! $this->isLoggedIn ()) {
			throw new CRMAsteriskApiException ( 'Bitte vorher einen Login ausführen!' );
		}
		$command = "Action: Status" . self::END_OF_LINE . "Channel: " . $channel . self::DOUBLE_END_OF_LINE;
		$response = $this->_sendCommandToSocket ( $command, false );
		return $response;
	}
	
	public function prepareNumberForAsterisk($number) {
		$pattern = '/[[:space:]\/\+\.-]/';
		$replace = '';
		return preg_replace($pattern, $replace, $number);
	}
}